package logic

import (
	"context"
	"encoding/json"
	"useryk/model/mysql"
	"useryk/model/redisinit"

	"useryk/userrpc/internal/svc"
	"useryk/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductLikeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductLikeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductLikeListLogic {
	return &ProductLikeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductLikeListLogic) ProductLikeList(in *userrpc.ProductLikeListRequest) (*userrpc.ProductLikeListResponse, error) {
	// todo: add your logic here and delete this line

	scores := redisinit.Rdb.ZRevRangeWithScores("liek", 0, 9)
	var product mysql.Product
	mysql.DB.Find(&product)
	var lists []*userrpc.ProductLikeList
	for _, z := range scores.Val() {
		json.Unmarshal([]byte(z.Member.(string)), &product)
		list := userrpc.ProductLikeList{
			Id:           int64(product.ID),
			Categorieid:  product.Categorieid,
			Num:          product.Num,
			Name:         product.Name,
			Categorienum: product.Categorienum,
			Productprice: product.Productprice,
			Costprice:    product.Costprice,
			Image:        product.Image,
			Like:         int64(z.Score),
		}
		lists = append(lists, &list)
	}

	return &userrpc.ProductLikeListResponse{
		List: lists,
	}, nil
}
